package com.liuhaoqi.raletiveblog.controller;

import com.github.pagehelper.PageInfo;
import com.liuhaoqi.raletiveblog.entity.Memory;
import com.liuhaoqi.raletiveblog.entity.Tag;
import com.liuhaoqi.raletiveblog.entity.dto.BlogDto;
import com.liuhaoqi.raletiveblog.service.BlogService;
import com.liuhaoqi.raletiveblog.service.CommentService;
import com.liuhaoqi.raletiveblog.service.MemoryService;
import com.liuhaoqi.raletiveblog.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author liuhaoqi
 * @Date 2020/4/18 17:05
 * @Version 1.0
 */
@Controller
public class HomeController {
    @Autowired
    BlogService blogService;
    @Autowired
    TagService tagService;
    @Autowired
    MemoryService memoryService;
    @Autowired
    CommentService commentService;
    @GetMapping("/")
    public String toHome(Model model, @RequestParam(defaultValue = "1",value = "pageNum") String pageNum,
                         @RequestParam(defaultValue = "10",value = "pageSize") String pageSize) {
        //请求后端数据库查询得到所有博客数据并返回给前端
        PageInfo<BlogDto> blogList = blogService.getBlogList(pageNum, pageSize);
        List<BlogDto> blogDtos = blogService.getBlogListNoFenYe();
        List<Tag> tagList = tagService.getTagList();
        //查询得到一路走来返回给前端
        List<Memory> memoryList = memoryService.getMemoryList();
        List<Memory> list =new ArrayList<>();
        for (int i = 0; i <3 ; i++) {
            list.add(memoryList.get(i));
        }
        model.addAttribute("memmory",list);
        model.addAttribute("tags",tagList);
        model.addAttribute("blogsNoFenYe",blogDtos);
        model.addAttribute("blogs",blogList);
        //得到文章总数，标签总数，留言总数，评论总数
        int blogsnum = blogService.getBlogsnum();
        int tagsnum = tagService.getTagsnum();
        int commentsnum1 = commentService.getCommentsnum1();
        int commentsnum2 = commentService.getCommentsnum2();
        model.addAttribute("blogsnum",blogsnum);
        model.addAttribute("tagsnum",tagsnum);
        model.addAttribute("commentsnum1",commentsnum1);
        model.addAttribute("commentsnum2",commentsnum2);
        return "home";
    }

    @GetMapping("/home")
    public String toHome_1(Model model,@RequestParam(defaultValue = "1",value = "pageNum") String pageNum,
                           @RequestParam(defaultValue = "10",value = "pageSize") String pageSize) {
        //请求后端数据库查询得到所有博客数据并返回给前端
        PageInfo<BlogDto> blogList = blogService.getBlogList(pageNum, pageSize);
        List<BlogDto> blogDtos = blogService.getBlogListNoFenYe();
        List<Tag> tagList = tagService.getTagList();
        //查询得到一路走来返回给前端
        List<Memory> memoryList = memoryService.getMemoryList();
        List<Memory> list =new ArrayList<>();
        for (int i = 0; i <3 ; i++) {
            list.add(memoryList.get(i));
        }
        model.addAttribute("memmory",list);
        model.addAttribute("tags",tagList);
        model.addAttribute("blogsNoFenYe",blogDtos);
        model.addAttribute("blogs",blogList);
        //得到文章总数，标签总数，留言总数，评论总数
        int blogsnum = blogService.getBlogsnum();
        int tagsnum = tagService.getTagsnum();
        int commentsnum1 = commentService.getCommentsnum1();
        int commentsnum2 = commentService.getCommentsnum2();
        model.addAttribute("blogsnum",blogsnum);
        model.addAttribute("tagsnum",tagsnum);
        model.addAttribute("commentsnum1",commentsnum1);
        model.addAttribute("commentsnum2",commentsnum2);
        return "home";
    }






}
